package com.platform.modular.vehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.core.exception.ServiceException;
import com.platform.core.factory.PageFactory;
import com.platform.core.pojo.page.PageResult;
import com.platform.modular.vehicle.entity.AutohomeVehicleModel;
import com.platform.modular.vehicle.enums.AutohomeVehicleModelExceptionEnum;
import com.platform.modular.vehicle.mapper.AutohomeVehicleModelMapper;
import com.platform.modular.vehicle.param.AutohomeVehicleModelParam;
import com.platform.modular.vehicle.service.AutohomeVehicleModelService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车型表service接口实现类
 *
 * @author gyp
 * @date 2025-01-13 14:31:03
 */
@Service
public class AutohomeVehicleModelServiceImpl extends ServiceImpl<AutohomeVehicleModelMapper, AutohomeVehicleModel> implements AutohomeVehicleModelService {

    @Override
    public PageResult<AutohomeVehicleModel> page(AutohomeVehicleModelParam autohomeVehicleModelParam) {
        QueryWrapper<AutohomeVehicleModel> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(autohomeVehicleModelParam)) {

            // 根据系统id 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getSystemId())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getSystemId, autohomeVehicleModelParam.getSystemId());
            }
            // 根据汽车之家车型id 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getQczjModelId())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getQczjModelId, autohomeVehicleModelParam.getQczjModelId());
            }
            // 根据品牌id 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getBrandId())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getBrandId, autohomeVehicleModelParam.getBrandId());
            }
            // 根据品牌名称 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getBrandName())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getBrandName, autohomeVehicleModelParam.getBrandName());
            }
            // 根据车系id 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getSeriesId())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getSeriesId, autohomeVehicleModelParam.getSeriesId());
            }
            // 根据车系名称 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getSeriesName())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getSeriesName, autohomeVehicleModelParam.getSeriesName());
            }
            // 根据车型名称 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getModelName())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getModelName, autohomeVehicleModelParam.getModelName());
            }
            // 根据由于车辆型号是从车行168（大搜车旗下）爬取，所以用此ID与其对应。 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getDscModelId())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getDscModelId, autohomeVehicleModelParam.getDscModelId());
            }
            // 根据车型配置信息 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getModelJson())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getModelJson, autohomeVehicleModelParam.getModelJson());
            }
            // 根据车型编码 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getModelCode())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getModelCode, autohomeVehicleModelParam.getModelCode());
            }
            // 根据外观颜色 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getAppearanceColor())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getAppearanceColor, autohomeVehicleModelParam.getAppearanceColor());
            }
            // 根据内饰颜色 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getInteriorColor())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getInteriorColor, autohomeVehicleModelParam.getInteriorColor());
            }
            // 根据销售类型 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getSaleType())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getSaleType, autohomeVehicleModelParam.getSaleType());
            }
            // 根据年款 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getModelYear())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getModelYear, autohomeVehicleModelParam.getModelYear());
            }
            // 根据排量(车型首页) 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getExhaust())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getExhaust, autohomeVehicleModelParam.getExhaust());
            }
            // 根据变速箱（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getShiftBox())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getShiftBox, autohomeVehicleModelParam.getShiftBox());
            }
            // 根据上市日期 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getLaunchDate())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getLaunchDate, autohomeVehicleModelParam.getLaunchDate());
            }
            // 根据车身结构 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getBodyTypeName())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getBodyTypeName, autohomeVehicleModelParam.getBodyTypeName());
            }
            // 根据发动机类型 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getEngineTypeName())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getEngineTypeName, autohomeVehicleModelParam.getEngineTypeName());
            }
            // 根据级别（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getCarGrade())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getCarGrade, autohomeVehicleModelParam.getCarGrade());
            }
            // 根据汽车类型 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getCarType())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getCarType, autohomeVehicleModelParam.getCarType());
            }
            // 根据子品牌id 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getSubBrandNo())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getSubBrandNo, autohomeVehicleModelParam.getSubBrandNo());
            }
            // 根据子品牌名称 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getSubBrandName())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getSubBrandName, autohomeVehicleModelParam.getSubBrandName());
            }
            // 根据指导价 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getGuidingPrice())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getGuidingPrice, autohomeVehicleModelParam.getGuidingPrice());
            }
            // 根据环保标准（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getEmissionStandard())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getEmissionStandard, autohomeVehicleModelParam.getEmissionStandard());
            }
            // 根据综合油耗(工信部)（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getOilConsumption())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getOilConsumption, autohomeVehicleModelParam.getOilConsumption());
            }
            // 根据最大功率（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getMaxPower())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getMaxPower, autohomeVehicleModelParam.getMaxPower());
            }
            // 根据最大扭矩（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getMaxTorsion())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getMaxTorsion, autohomeVehicleModelParam.getMaxTorsion());
            }
            // 根据能源类型（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getEnergyType())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getEnergyType, autohomeVehicleModelParam.getEnergyType());
            }
            // 根据快充时间（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getFastChargingTime())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getFastChargingTime, autohomeVehicleModelParam.getFastChargingTime());
            }
            // 根据慢充时间（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getSlowChargingTime())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getSlowChargingTime, autohomeVehicleModelParam.getSlowChargingTime());
            }
            // 根据续航里程（车型首页） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getEnduranceMileage())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getEnduranceMileage, autohomeVehicleModelParam.getEnduranceMileage());
            }
            // 根据0,未删除 1,已删除 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getDeleteFlag())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getDeleteFlag, autohomeVehicleModelParam.getDeleteFlag());
            }
            // 根据 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getCreateBy())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getCreateBy, autohomeVehicleModelParam.getCreateBy());
            }
            // 根据 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getUpdateBy())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getUpdateBy, autohomeVehicleModelParam.getUpdateBy());
            }
            // 根据 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getRemark())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getRemark, autohomeVehicleModelParam.getRemark());
            }
            // 根据 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getBmpId())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getBmpId, autohomeVehicleModelParam.getBmpId());
            }
            // 根据数据状态（1：相似度较高，不用合并；2：匹配度较低，定义为新增；3：需要手动合并; 4:数据无效;6.在中台查出多条数据） 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getDataStatus())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getDataStatus, autohomeVehicleModelParam.getDataStatus());
            }
            // 根据数据存储规则（1:车系不存在，新增数据至中台；2：年款相同，指导价相同，定义为同一车型，与中台车型建立关系 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getDataRule())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getDataRule, autohomeVehicleModelParam.getDataRule());
            }
            // 根据最高相似度 查询
            if (ObjectUtil.isNotEmpty(autohomeVehicleModelParam.getMostSimilar())) {
                queryWrapper.lambda().eq(AutohomeVehicleModel::getMostSimilar, autohomeVehicleModelParam.getMostSimilar());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<AutohomeVehicleModel> list(AutohomeVehicleModelParam autohomeVehicleModelParam) {
        return this.list();
    }

    @Override
    public void add(AutohomeVehicleModelParam autohomeVehicleModelParam) {
        AutohomeVehicleModel autohomeVehicleModel = new AutohomeVehicleModel();
        BeanUtil.copyProperties(autohomeVehicleModelParam, autohomeVehicleModel);
        this.save(autohomeVehicleModel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<AutohomeVehicleModelParam> autohomeVehicleModelParamList) {
        autohomeVehicleModelParamList.forEach(autohomeVehicleModelParam -> {
            this.removeById(autohomeVehicleModelParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(AutohomeVehicleModelParam autohomeVehicleModelParam) {
        AutohomeVehicleModel autohomeVehicleModel = this.queryAutohomeVehicleModel(autohomeVehicleModelParam);
        BeanUtil.copyProperties(autohomeVehicleModelParam, autohomeVehicleModel);
        this.updateById(autohomeVehicleModel);
    }

    @Override
    public AutohomeVehicleModel detail(AutohomeVehicleModelParam autohomeVehicleModelParam) {
        return this.queryAutohomeVehicleModel(autohomeVehicleModelParam);
    }

    /**
     * 获取车型表
     *
     * @author gyp
     * @date 2025-01-13 14:31:03
     */
    private AutohomeVehicleModel queryAutohomeVehicleModel(AutohomeVehicleModelParam autohomeVehicleModelParam) {
        AutohomeVehicleModel autohomeVehicleModel = this.getById(autohomeVehicleModelParam.getId());
        if (ObjectUtil.isNull(autohomeVehicleModel)) {
            throw new ServiceException(AutohomeVehicleModelExceptionEnum.NOT_EXIST);
        }
        return autohomeVehicleModel;
    }

}
